package com.qd.chatui.adapter.holder;

import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.qd.chatui.R;
import com.qd.chatui.adapter.ChatAdapter;
import com.qd.chatui.enity.MessageInfo;
import com.qd.chatui.util.MyUtils;
import com.qd.chatui.util.Utils;
import com.qd.chatui.widget.BubbleImageView;
import com.qd.chatui.widget.EmotionInputDetector;
import com.qd.chatui.widget.GifTextView;
import com.qd.common.commonutils.DateUtils;
import com.qd.common.commonutils.GlideRoundTransformUtil;
import com.qd.common.commonutils.ImageLoaderUtils;

/**
 * 作者：Rance on 2016/11/29 10:47
 * 邮箱：rance935@163.com
 */
public class ChatAcceptViewHolder extends BaseViewHolder<MessageInfo> {

    TextView chatItemDate;
    ImageView chatItemHeader;
    GifTextView chatItemContentText;
    BubbleImageView chatItemContentImage;
    ImageView chatItemVoice;
    LinearLayout chatItemLayoutContent;
    TextView chatItemVoiceTime;
    TextView chatTvName;
    private ChatAdapter.onItemClickListener onItemClickListener;
    private Handler handler;
    public boolean isShowName = false;

    public ChatAcceptViewHolder(ViewGroup parent, ChatAdapter.onItemClickListener onItemClickListener, Handler handler, boolean isShowName) {
        super(parent, R.layout.item_chat_accept);
        initView();
        this.onItemClickListener = onItemClickListener;
        this.handler = handler;
        this.isShowName = isShowName;
    }

    private void initView() {
        chatItemDate = itemView.findViewById(R.id.chat_item_date);
        chatItemHeader = itemView.findViewById(R.id.chat_item_header);
        chatItemContentText = itemView.findViewById(R.id.chat_item_content_text);
        chatItemContentImage = itemView.findViewById(R.id.chat_item_content_image);
        chatItemVoice = itemView.findViewById(R.id.chat_item_voice);
        chatItemLayoutContent = itemView.findViewById(R.id.chat_item_layout_content);
        chatItemVoiceTime = itemView.findViewById(R.id.chat_item_voice_time);
        chatTvName = itemView.findViewById(R.id.tvChatName);
    }

    @Override
    public void setData(MessageInfo data) {
        // chatItemDate.setText(data.getSendTime() != null ? data.getSendTime() : "");
        if (data.isShowTimeStamp()) {
            chatItemDate.setText(DateUtils.LongFormatTimeChatLayout(data.getSendTime()));
            chatItemDate.setVisibility(View.VISIBLE);
        } else {
            chatItemDate.setVisibility(View.GONE);
        }


//        Glide.with(getContext()).load(data.getHeader()).placeholder(com.qd.common.R.drawable.ic_image_loading)
//                .error(com.qd.common.R.drawable.ic_empty_picture)
//                .transform(new CenterCrop(getContext()), new GlideRoundTransformUtil(getContext()))
//                .thumbnail(0.5f).into(chatItemHeader);
        MyUtils.displayUserHeader(getContext(), chatItemHeader, data.getHeader(), data.getRealName());
        if (isShowName) {
            chatTvName.setVisibility(View.VISIBLE);
            chatTvName.setText(data.getRealName());
        } else {
            chatTvName.setVisibility(View.GONE);
        }

        chatItemHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onHeaderClick(getDataPosition());
            }
        });
        String type = data.getMsgType();
        switch (type) {
            case EmotionInputDetector.sendType_Text:
                chatItemContentText.setSpanText(handler, data.getContent(), true);
                chatItemVoice.setVisibility(View.GONE);
                chatItemContentText.setVisibility(View.VISIBLE);
                chatItemLayoutContent.setVisibility(View.VISIBLE);
                chatItemVoiceTime.setVisibility(View.GONE);
                chatItemContentImage.setVisibility(View.GONE);
                break;
            case EmotionInputDetector.sendType_Audio:
                chatItemVoice.setVisibility(View.VISIBLE);
                chatItemLayoutContent.setVisibility(View.VISIBLE);
                chatItemContentText.setVisibility(View.GONE);
                chatItemVoiceTime.setVisibility(View.VISIBLE);
                chatItemContentImage.setVisibility(View.GONE);
                String[] strs = null;
                try {
                    strs = data.getContent().split("$");
                    if (null != strs) {
                        chatItemVoiceTime.setText(Utils.formatTime(Long.parseLong(strs[1])));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                chatItemLayoutContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onVoiceClick(chatItemVoice, getDataPosition());
                    }
                });
                break;
            case EmotionInputDetector.sendType_Img:
                chatItemVoice.setVisibility(View.GONE);
                chatItemLayoutContent.setVisibility(View.GONE);
                chatItemVoiceTime.setVisibility(View.GONE);
                chatItemContentText.setVisibility(View.GONE);
                chatItemContentImage.setVisibility(View.VISIBLE);
                ImageLoaderUtils.displaySmallPhoto(getContext(), chatItemContentImage, data.getContent());
                chatItemContentImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onImageClick(chatItemContentImage, getDataPosition());
                    }
                });
                break;
        }
    }
}
